//
//  AppDelegate.swift
//  HHWeibo
//
//  Created by hehuafeng on 2017/2/14.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // 0. 授权设置应用程序提醒数字
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound], completionHandler: { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            })
            
        }else {
            let settings = UIUserNotificationSettings.init(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        

        // 1. 创建窗口
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        // 2. 设置窗口根控制器
        let rootVc = HHMainViewController()
        
        window?.rootViewController = rootVc
        
        // 3. 显示窗口
        window?.makeKeyAndVisible()
        
        // 4. 加载配置信息
        loadAppInfo()
        
        return true
    }

}

// MARK: - 加载json
extension AppDelegate {
    
    func loadAppInfo() {
        DispatchQueue.global().async {
            // 1. 加载bundle中的数据
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            
            // 2. 获取沙盒路径
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (path as NSString).appendingPathComponent("main.json")
            
            // 3. 写入到沙盒中
            data?.write(toFile: jsonPath, atomically: true)
//            print("应用程序加载完毕: \(jsonPath)")
        }
    }
    
    /// 授权设置应用程序提醒数字
    func unreadCountAuthorize() {
        
        
    }
}
